All files / platform platform.ts

85.71% Statements 12/14
50% Branches 2/4
100% Functions 4/4
84.62% Lines 11/13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78                                        2x                                                                 2x   2x 2x     2x     2x 2869x     2869x   2x           2x 1791x    
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import { DatabaseId, DatabaseInfo } from '../core/database_info';
import { ProtoByteString } from '../core/types';
import { Connection } from '../remote/connection';
import { JsonProtoSerializer } from '../remote/serializer';
import { fail } from '../util/assert';
import { AnyJs } from '../util/misc';
 
/**
 * Provides a common interface to load anything platform dependent, e.g.
 * the connection implementation.
 *
 * An implementation of this must be provided at compile time for the platform.
 */
export interface Platform {
  loadConnection(databaseInfo: DatabaseInfo): Promise<Connection>;
  newSerializer(databaseId: DatabaseId): JsonProtoSerializer;
 
  /** Formats an object as a JSON string, suitable for logging. */
  formatJSON(value: AnyJs): string;
 
  /** Converts a Base64 encoded string to a binary string. */
  atob(encoded: string): string;
 
  /** Converts a binary string to a Base64 encoded string. */
  btoa(raw: string): string;
 
  /** True if and only if the Base64 conversion functions are available. */
  readonly base64Available: boolean;
 
  readonly emptyByteString: ProtoByteString;
}
 
/**
 * Provides singleton helpers where setup code can inject a platform at runtime.
 * setPlatform needs to be set before Firestore is used and must be set exactly
 * once.
 */
export class PlatformSupport {
  private static platform: Platform;
  static setPlatform(platform: Platform): void {
    Iif (PlatformSupport.platform) {
      fail('Platform already defined');
    }
    PlatformSupport.platform = platform;
  }
 
  static getPlatform(): Platform {
    Iif (!PlatformSupport.platform) {
      fail('Platform not set');
    }
    return PlatformSupport.platform;
  }
}
 
/**
 * Returns the representation of an empty "proto" byte string for the
 * platform.
 */
export function emptyByteString(): ProtoByteString {
  return PlatformSupport.getPlatform().emptyByteString;
}